<template>
  <!-- 只能有一对div标签 -->
  <div>

    <el-container>
      <el-main>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="用户名" prop="username">
            <el-input type="text" v-model="ruleForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="ruleForm.password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
            <el-button @click="resetForm('ruleForm')">取消</el-button>
          </el-form-item>
        </el-form>
      </el-main>
    </el-container>
    <Footer></Footer>
  </div>
</template>

<script>
import Element from 'element-ui'
import Footer from "@/components/Footer";
import Header from "@/components/Header";
export default {
  name: "Login",
  components: {Header, Footer},
  data() {
    return {
      activeIndex: '4',
      ruleForm: {
        username: 'carpBlog',
        password: '111111'
      },
      rules: {
        userName: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'change'}
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const  _this =this
          this.$axios.post("/login",this.ruleForm).then(res => {
           /* console.log(res.headers)
            console.log(res)*/
            const jwt =res.headers['authorization']
            const  userInfo =res.data.data
            console.log(userInfo)

            //把数据共享出去
            _this.$store.commit("SET_TOKEN",jwt)
            _this.$store.commit("SET_USERINFO",userInfo)
            //获取
            console.log(_this.$store.getters.getUser)

            //跳转
            _this.$router.push("/blogs")

            Element.Message.success("登录成功",{duration:3*1000})

          })
        } else {
          Element.Message.error("登陆失败",{duration:3*1000})
          return false;
        }
      });
    },
    resetForm(formName) {
      const  _this =this
      _this.$router.push("/blogs")
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>
 .el-footer {
  background-color: #B3C0D1;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-aside {
  background-color: #D3DCE6;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  color: #333;
  text-align: center;
  line-height: 160px;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}

.logo {
  width: 20%;
  height: 80%;
  margin-top: 5px;
}

.demo-ruleForm {
  max-width: 500px;
  margin: 0 auto;
}

</style>